package main

import (
	"context"
	"fmt"
	"github.com/wailsapp/wails/v2/pkg/runtime"
	"ideatools/application/constant"
	"ideatools/application/constant/enums"
	ctx2 "ideatools/application/core/ctx"
	"ideatools/application/core/entry"
	"ideatools/application/db/dbservice"
	"ideatools/application/pkgs/mylog"
	"ideatools/application/pkgs/syst"
	"ideatools/setting"
	runtime2 "runtime"
	"time"
)

// App struct
type App struct {
	ctx context.Context
}

// NewApp creates a new App application struct
func NewApp() *App {
	return &App{}
}

// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
	a.ctx = ctx
	ctx2.SetCtx(&a.ctx) //赋值给全局变量
	// go a.timer()        //测试由go向前端触发事件 --pass
}

func (a *App) SelectDir() string {
	//if a.ctx == nil {
	//	return "a.ctx == nil"
	//}
	filters := []runtime.FileFilter{
		{"Image Files", "*.jpg;*.png;*.*"},
	}
	opts := runtime.OpenDialogOptions{
		DefaultDirectory: fmt.Sprintf("C:\\"),
		DefaultFilename:  fmt.Sprintf("a"),
		Title:            fmt.Sprintf("请选择目录"),
		Filters:          filters,
	}
	res, err := runtime.OpenDirectoryDialog(a.ctx, opts)

	if err != nil {
		return err.Error()
	}

	return res
}

// Greet returns a greeting for the given name
func (a *App) Greet(name string) string {
	return fmt.Sprintf("Hello %s, It's show time!", name)
}

// Greet1 returns a greeting for the given name
func (a *App) Greet1(name string) string {
	return fmt.Sprintf("Greet1:Hello %s, It's show time!", name)
}

// Greet2  returns a greeting for the given name
func (a *App) Greet2(name string) string {
	return fmt.Sprintf("Greet2:Hello %s, It's show time!", name)
}

func (a *App) timer() {
	i := 0
	for {
		i += 1
		time.Sleep(time.Millisecond * time.Duration(2000))
		runtime.EventsEmit(a.ctx, "event1", i)
	}

}

// MouseEventCallback 鼠标事件回调
func (a *App) MouseEventCallback(eventName constant.MouseEvent, MouseButton string) {
	if eventName == constant.MouseEventLongClick && MouseButton == constant.MouseButtonSide {
		if !runtime.WindowIsMinimised(a.ctx) {
			runtime.WindowMinimise(a.ctx) //最小化
		} else {
			runtime.WindowUnminimise(a.ctx) //恢复正常
		}

	}
}

func (a *App) KeyboardEventCallback(CombineKey string) {

}

func (a App) Toast(content string) {
	runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
		Type:          "OK",
		Title:         "Title",
		Message:       content,
		Buttons:       nil,
		DefaultButton: "",
		CancelButton:  "",
		Icon:          nil,
	})
}

// HotkeyIsUse 检测一个组合快捷键是否已经被使用
func (s *App) HotkeyIsUse(combineKey string) bool {

	return entry.HotKeyIsUse(combineKey)
}

func (b *App) beforeClose(ctx context.Context) (prevent bool) {
	if runtime2.GOOS == syst.SystemTypeWindows {
		dialog, err := runtime.MessageDialog(ctx, runtime.MessageDialogOptions{
			Type:    runtime.QuestionDialog,
			Title:   "退出程序?",
			Message: "是否确定退出程序?",
		})

		if err != nil {
			return false
		}
		return dialog != "Yes"
	}
	return false

}

func (s *App) setWinSize(winType int) {
	w, h := 0, 0
	switch winType {
	case enums.WindowsTypeMessage:
		w = dbservice.Conf().GetNumber(constant.ConfigKeyWinHeightMessage)
		h = dbservice.Conf().GetNumber(constant.ConfigKeyWinHeightMessage)
		if w == 0 {
			w = 800
		}
		if h == 0 {
			h = 700
		}
	case enums.WindowsTypeQuickTools:
		w = dbservice.Conf().GetNumber(constant.ConfigKeyWinHeightQuick)
		h = dbservice.Conf().GetNumber(constant.ConfigKeyWinHeightQuick)
		if w == 0 {
			w = 400
		}
		if h == 0 {
			h = 500
		}
	default:
		mylog.Debug("defalult din swiz")
		w = setting.MainWinWeigh
		h = setting.MainWinHeight
	}

	runtime.WindowSetSize(s.ctx, w, h)

}
